package com.st.event.demo.proxy.bean;

import com.st.event.demo.post.OrderFinishTriggerListener;
import com.st.event.demo.source.EventAttributeSource;
import com.st.event.demo.strategy.EventStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.util.Assert.notNull;

public abstract class EventProviderBeanSupport implements InitializingBean {

    private Map<String, OrderFinishTriggerListener> listenerMap = new HashMap<>(8);

    private ApplicationContext ac;

    private EventAttributeSource eventAttributeSource;

    private EventStrategy eventStrategy;

    protected EventProviderBeanSupport() {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行9-----------");
        notNull(this.ac, "application is not start!");
        listenerMap = ac.getBeansOfType(OrderFinishTriggerListener.class);
        if (this.eventAttributeSource == null) {
            this.eventAttributeSource = ac.getBean(EventAttributeSource.class);
            notNull(this.eventAttributeSource, "Property 'eventAttributeSource' are required!");
            this.eventStrategy = ac.getBean(EventStrategy.class);
            notNull(this.eventAttributeSource, "Property 'eventStrategy' are required!");
        }
    }

    public Map<String, OrderFinishTriggerListener> getListenerMap() {
        return listenerMap;
    }

    public void setListenerMap(Map<String, OrderFinishTriggerListener> listenerMap) {
        this.listenerMap = listenerMap;
    }

    public ApplicationContext getAc() {
        return ac;
    }

    public void setAc(ApplicationContext ac) {
        this.ac = ac;
    }

    public EventAttributeSource getEventAttributeSource() {
        return eventAttributeSource;
    }

    public void setEventAttributeSource(EventAttributeSource eventAttributeSource) {
        this.eventAttributeSource = eventAttributeSource;
    }

    public EventStrategy getEventStrategy() {
        return eventStrategy;
    }

    public void setEventStrategy(EventStrategy eventStrategy) {
        this.eventStrategy = eventStrategy;
    }
}
